/**
 * @file XFooterView.java
 * @create Mar 31, 2012 9:33:43 PM
 * @author Maxwin
 * @description XListView's footer
 */
package www.com.library.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import www.com.common.library.R;

public class RecyclerViewFooter extends RelativeLayout {

    public final static int STATE_NORMAL = 0;
    public final static int STATE_READY = 1;
    public final static int STATE_LOADING = 2;
    public final static int STATE_END = 3;//表示没有数据了

    private Context mContext;

    private View mContentView;
    private View mProgress;
    private TextView mHintView;

    private String mStateEndText = "";

    public RecyclerViewFooter(Context context) {
        super(context);
        initView(context);
    }

    public RecyclerViewFooter(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    /**
     * 没有更多数据的文案
     */
    public void setStateEndText(String stateEndText) {
        mStateEndText = stateEndText;
    }

    public void setState(int state) {
        mHintView.setVisibility(View.GONE);
        mProgress.setVisibility(View.GONE);
        if (state == STATE_READY) {
            mHintView.setVisibility(View.VISIBLE);
            mHintView.setText(R.string.listview_footer_hint_ready);
        } else if (state == STATE_LOADING) {
            mProgress.setVisibility(View.VISIBLE);
        } else if (state == STATE_NORMAL) {
            mHintView.setVisibility(View.VISIBLE);
            mHintView.setText(R.string.listview_footer_hint_normal);
        } else if (state == STATE_END) {
            mHintView.setVisibility(View.VISIBLE);
            if (null != mStateEndText && mStateEndText.length() > 0) {
                mHintView.setText(mStateEndText);
            } else {
                mHintView.setText(R.string.listview_footer_hint_nomore);
            }
        }
    }

    public void setBottomMargin(int height) {
        if (height < 0) return;
        LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
        lp.bottomMargin = height;
        mContentView.setLayoutParams(lp);
    }

    public int getBottomMargin() {
        LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
        return lp.bottomMargin;
    }


    /**
     * normal status
     */
    public void normal() {
        mHintView.setVisibility(View.VISIBLE);
        mProgress.setVisibility(View.GONE);
    }


    /**
     * loading status
     */
    public void loading() {
        mHintView.setVisibility(View.GONE);
        mProgress.setVisibility(View.VISIBLE);
    }

    /**
     * hide footer when disable pull load more
     */
    public void hide() {
        LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
        lp.height = 0;
        mContentView.setLayoutParams(lp);
    }

    /**
     * show footer
     */
    public void show() {
        LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
        lp.height = LayoutParams.WRAP_CONTENT;
        mContentView.setLayoutParams(lp);
    }

    private void initView(Context context) {
        mContext = context;
        RelativeLayout moreView = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.listview_footer, null);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, 0, 0, 0);
        this.setLayoutParams(lp);
        this.setPadding(0, 0, 0, 0);

        addView(moreView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        setGravity(Gravity.BOTTOM);

        mContentView = moreView.findViewById(R.id.xlistview_footer_content);
        mProgress = moreView.findViewById(R.id.xlistview_footer_progress);
        mHintView = (TextView) moreView.findViewById(R.id.xlistview_footer_hint_textview);
    }
}
